﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Video;
using IAlarm;

namespace TestMultiVideo
{
    public partial class Form1 : Form
    {
        Dictionary<string, List<string>> devices;
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.devices = VideoBase.GetAreaDeviceList();

            foreach (KeyValuePair<string, List<string>> area in devices)
            {
                TreeNode TNArea = new TreeNode(area.Key);
                foreach (string device in area.Value)
                {
                    TNArea.Nodes.Add(device);
                }
                tVAreaDevices.Nodes.Add(TNArea);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            UserVideo.SelectAll(true);
            UserVideo.CloseVideos();
            UserVideo.LogoutEncoders();

            base.OnClosing(e);
        }

        private void tVAreaDevices_DoubleClick(object sender, EventArgs e)
        {
            TreeNode TNDevice = tVAreaDevices.SelectedNode;

            if (TNDevice == null || TNDevice.Level != 1 || TNDevice.Text.StartsWith("[不可用]")) return;

            if (TNDevice.Tag == null)
            {//初始化
                UserVideo video = new UserVideo();
                video.VideoOpened += new EventHandler(video_VideoOpened);
                if (video.Initialize(TNDevice.Parent.Text, TNDevice.Text)
                    && video.LoginEncoder() && video.OpenVideo())
                {
                    video.VideoClosed += new EventHandler(video_VideoClosed);
                    video.SelectedChanged += new EventHandler(video_SelectedChanged);
                    //video.IsExpandButton = true;
                    video.BorderStyle = BorderStyle.FixedSingle;
                    TNDevice.Tag = video;
                    Form f = new Form();
                    f.Text = video.AreaName + " - " + video.DeviceName;
                    f.ControlBox = false;
                    f.TopMost = true;
                    f.ClientSize = new Size(352, 288);
                    f.FormBorderStyle = FormBorderStyle.FixedSingle;
                    f.Show();
                    video.AnalyzeShow = f;
                    
                    UserVideo.SelectDevice(video.AreaName, video.DeviceName, false);
                }
                else
                {
                    TNDevice.Text = "[不可用]" + TNDevice.Text;
                    video.Dispose();
                }
            }
            else
            {
                UserVideo video = TNDevice.Tag as UserVideo;

                if (video != null && video.IsLogined)
                {
                    if (video.IsOpened)
                    {
                        video.CloseVideo();
                        if (video.AnalyzeShow != null) video.AnalyzeShow.Hide();
                    }
                    else
                    {
                        video.OpenVideo();
                        if (video.AnalyzeShow != null) video.AnalyzeShow.Show();
                    }
                }
            }

        }

        private void tVAreaDevices_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode TNDevice = tVAreaDevices.SelectedNode;

            if (TNDevice != null)
            {
                if (TNDevice.Level == 0)
                {
                    UserVideo.SelectArea(TNDevice.Text, isControlDown);
                }
                else if (TNDevice.Level == 1)
                {
                    UserVideo.SelectDevice(TNDevice.Parent.Text, TNDevice.Text, isControlDown);
                }
            }

        }

        bool isControlDown = false;
        private void tVAreaDevices_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
                isControlDown = true;
        }

        private void tVAreaDevices_KeyUp(object sender, KeyEventArgs e)
        {
            if (!e.Control)
                isControlDown = false;
        }

        void video_VideoOpened(object sender, EventArgs e)
        {
            LayoutVideo();
        }

        void video_VideoClosed(object sender, EventArgs e)
        {
            LayoutVideo();
        }

        private void pVideo_Resize(object sender, EventArgs e)
        {
            LayoutVideo();
        }

        void LayoutVideo()
        {
            pVideo.Controls.Clear();
            List<UserVideo> videos = UserVideo.GetStateVideos(StateType.Video);
            int line = (int)(Math.Ceiling(Math.Sqrt(videos.Count)));

            for (int i = 0; i < videos.Count; i++)
            {
                videos[i].Size = new Size(this.pVideo.Width / line, this.pVideo.Height / line);
                videos[i].Location = new Point(this.pVideo.Width * (i % line) / line, this.pVideo.Height * (int)(i / line) / line);
                pVideo.Controls.Add(videos[i]);
            }

            videos.Clear();
            videos = null;

        }

        void video_SelectedChanged(object sender, EventArgs e)
        {
            UserVideo video = sender as UserVideo;

            foreach (TreeNode TNArea in tVAreaDevices.Nodes)
            {
                if (video.AreaName == TNArea.Text)
                {
                    TNArea.Expand();
                    //if (tVAreaDevices.SelectedNode != TNArea)
                    //{
                    //    foreach (TreeNode TNDevice in TNArea.Nodes)
                    //    {
                    //        if (TNDevice.Text == video.DeviceName)
                    //        {
                    //            if (video.IsSelected && tVAreaDevices.SelectedNode != TNDevice)
                    //                tVAreaDevices.SelectedNode = TNDevice;
                    //            else if (!video.IsSelected && tVAreaDevices.SelectedNode == TNDevice)
                    //                tVAreaDevices.SelectedNode = null;
                    //            break;
                    //        }
                    //    }
                    //}

                    break;
                }
            }
        }

        private void bUp_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Up((byte)hSPSpeed.Value);
        }

        private void bRight_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Right((byte)hSPSpeed.Value);
        }

        private void bLeft_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Left((byte)hSPSpeed.Value);

        }

        private void bDown_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Down((byte)hSPSpeed.Value);

        }

        private void bPanLeftTiltUp_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.LeftUp((byte)hSPSpeed.Value);
        }

        private void bPanRightTiltUp_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.RightUp((byte)hSPSpeed.Value);

        }

        private void bPanLeftTiltDown_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.LeftDown((byte)hSPSpeed.Value);

        }

        private void bPanRightTiltDown_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.RightDown((byte)hSPSpeed.Value);

        }

        private void bTele_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Tele();
        }

        private void bWide_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Wide();

        }

        private void bNear_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Near();

        }

        private void bFar_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Far();

        }

        private void bOpen_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Open();

        }

        private void bClose_MouseDown(object sender, MouseEventArgs e)
        {
            UserVideo.Close();

        }

        private void bPTZ_MouseUp(object sender, MouseEventArgs e)
        {
            UserVideo.Stop();

        }

        private void bSnap_Click(object sender, EventArgs e)
        {
            UserVideo.Snap();
        }

        private void bStartAnalyze_Click(object sender, EventArgs e)
        {
            UserVideo.StartAnalyzes();
        }

        private void bStopAnalyze_Click(object sender, EventArgs e)
        {
            UserVideo.StopAnalyzes();
        }

        private void bStartCruise_Click(object sender, EventArgs e)
        {
            UserVideo.StartCruises();

        }

        private void bStopCruise_Click(object sender, EventArgs e)
        {
            UserVideo.StopCruises();
        }

        private void bStartRecord_Click(object sender, EventArgs e)
        {
            UserVideo.StartRecords();
        }

        private void bStopRecord_Click(object sender, EventArgs e)
        {
            UserVideo.StopRecords();
        }




    }
}
